package com.freecloud.javabasics.design.proxy.mybatis.supper;

import com.freecloud.javabasics.design.proxy.mybatis.plugins.LoggerInterceptor;
import com.freecloud.javabasics.design.proxy.mybatis.plugins.PrintInterceptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 代理链包装，用于生成代理链
 * @Author: maomao
 * @Date: 2021-04-11 18:15
 */
public class FreeInterceptorChain {

    private final List<FreeInterceptor> interceptors = new ArrayList<>();

    {
        interceptors.add(new LoggerInterceptor());
        interceptors.add(new PrintInterceptor());
    }

    public Object pluginAll(Object target) {
        for (FreeInterceptor interceptor : interceptors) {
            target = interceptor.plugin(target);
        }
        return target;
    }

    public void addInterceptor(FreeInterceptor interceptor) {
        interceptors.add(interceptor);
    }

    public List<FreeInterceptor> getInterceptors() {
        return Collections.unmodifiableList(interceptors);
    }
}
